package com.easyfun.healthmagicbox.util;

import java.util.Random;

import android.content.Context;
import android.telephony.TelephonyManager;

public class PersonProfileCache {
	
	String username;
	String personID;
	Context context;
	
	private PersonProfileCache(Context _context)
	{
		context = _context;
	}
	private static PersonProfileCache instance = null;
	
	public static PersonProfileCache getInstance(Context _context)
	{
		if (instance == null)
		{
			instance = new PersonProfileCache(_context);
		}
		return instance;
	}
	
	
	
	public String getUsername(Context outcontext) {
		if (((username == null)||(username.length()==0)) && (outcontext != null))
		{
			username = SharedPreferencesUtil.getInstance(outcontext).getUsername();
		}else if (((username == null)||(username.length()==0)) && (context != null))
		{
			username = SharedPreferencesUtil.getInstance(context).getUsername();
		}
			
		return username;
	}

	public void setUsername(String iusername,Context icontext) {
		//can't be null;
		if ((iusername == null)||(icontext == null))
			return;
		
		context = icontext;
		username = iusername;
		if ((iusername != null)&&(iusername.length()!=0))
		{
			SharedPreferencesUtil.getInstance(context).setUsername(iusername);
		}
	}

	public String getPersonID(Context _context) {
		if ((personID == null)||(personID.length()==0))
		{
			personID = SharedPreferencesUtil.getInstance(_context).getPersonID();
		}
		return personID;
	}

	public void setPersonID(String ipersonID,Context icontext) {
		//can't be null;
		if ((ipersonID == null)||(icontext == null))
			return;
		context = icontext;
		personID = ipersonID;
		if ((ipersonID != null)&&(ipersonID.length()!=0))
		{
			SharedPreferencesUtil.getInstance(context).setPersonID(ipersonID);
		}
	}



	public static String generatePersonID(Context context)
	{
		int deviceBit = 9;
		int timeBit = 2;
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	    String head;
        String tel = tm.getDeviceId();
        if ((tel!=null) && (tel.length() > 0))
        	head = formatID(tel,deviceBit);
        else
        {
        	String deviceid = tm.getLine1Number();
        	if ((deviceid!=null) && (deviceid.length() > 0))
            	head = formatID(deviceid,deviceBit);
        	else
        	{
        		String imei = tm.getSimSerialNumber();
        		if ((imei!=null) && (imei.length() > 0))
                	head = formatID(imei,deviceBit);
            	else
            	{
            		String imsi = tm.getSubscriberId();
            		if ((imsi!=null) && (imsi.length() > 0))
                    	head = formatID(imsi,deviceBit);
            		else
            		{
            			head = formatID(String.valueOf(System.currentTimeMillis()),deviceBit);
            		}
            	}
        	}
        }
        
        Random random = new Random(System.currentTimeMillis());
        // total length is 15
        head = head +  formatID(String.valueOf(System.currentTimeMillis()),2) + formatID(String.valueOf(random.nextInt()),4);
        return head;     
        
	}
	

	
	private static String formatID(String deviceid, int length)
	{
		if (deviceid.length()>=length)
        	deviceid = deviceid.substring(deviceid.length()-length,deviceid.length());
        while (deviceid.length()<length)
        {
        	deviceid = deviceid + "0";
        }
        return deviceid;
	}
	
}
